package com.xqboss.apps.vo.order;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xqboss.apps.enums.market.MarketTypeEnum;
import com.xqboss.apps.enums.order.OutOrderStatusEnum;
import com.xqboss.apps.enums.order.OrderStatusEnum;
import com.xqboss.apps.enums.order.OrderTypeEnum;
import com.xqboss.apps.enums.order.RefundStatusEnum;
import com.xqboss.apps.enums.user.PayTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author Trent
 * @date 2023/8/29
 */
@Data
@ApiModel
public class AppOrderListVo {

    @ApiModelProperty("订单ID")
    private Long id;
    @ApiModelProperty("订单类型")
    private OrderTypeEnum orderType;
    @ApiModelProperty("市场类型")
    private MarketTypeEnum marketType;
    @ApiModelProperty(value = "买家用户ID")
    private Long buyerUserId;
    @ApiModelProperty(value = "买家用户昵称")
    private String buyerNick;
    @ApiModelProperty("商品数量")
    private Integer skuCnt;
    @ApiModelProperty("订单总金额（不包含优惠）")
    private BigDecimal totalAmount;
    @ApiModelProperty("应付金额（扣除优惠）")
    private BigDecimal paymentAmount;
    @ApiModelProperty("实际支付")
    private BigDecimal actualPaymentAmount;
    @ApiModelProperty("售后状态")
    private RefundStatusEnum refundStatus;
    @ApiModelProperty("下单时间")
    private LocalDateTime createTime;
    @ApiModelProperty("取消时间")
    private LocalDateTime cancelTime;
    @ApiModelProperty("订单过期时间")
    private LocalDateTime autoCancelTime;
    @ApiModelProperty("支付时间")
    private LocalDateTime payTime;
    @ApiModelProperty("支付方式")
    private PayTypeEnum payType;
    @ApiModelProperty("支付流水")
    private String payOrderNo;

    @ApiModelProperty("APP的订单状态")
    private OutOrderStatusEnum orderStatus;
    @ApiModelProperty("支付倒计时(单位：毫秒)")
    private Integer payCountDown;

    @ApiModelProperty("子订单信息")
    private List<AppOrderItemListVo> orderItems;


    /**
     * 订单实际状态
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    @Getter(AccessLevel.NONE)
    private OrderStatusEnum status;

    public OutOrderStatusEnum getOrderStatus() {
        return OutOrderStatusEnum.toOrderStatus(status);
    }

    public Integer getPayCountDown() {
        LocalDateTime now = LocalDateTime.now();
        if(autoCancelTime == null){
            return 0;
        }
        long countDown = LocalDateTimeUtil.between(now, autoCancelTime, ChronoUnit.MILLIS);
        if(countDown < 0){
            return 0;
        }
        return Math.toIntExact(countDown);
    }
}
